<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\BaseController;
use App\Http\Events\Admin\AdminEvent;
use App\Http\Model\Admin\Menu;
use app\Providers\PhpTreeServiceProvider;
use Config;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class MenuController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $menu = new Menu();
        $list = $menu->findAllByPaginate();
        return view('admin.menu', ['menu_list' => PhpTreeServiceProvider::makeTreeForHtml($list)]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $menu = new Menu();
        $list = $menu->findAllValid();
        return view('admin.menu_create', ['menu_list' => PhpTreeServiceProvider::makeTreeForHtml($list)]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = Input::except('_token');

        $validator = $this->formValidator($input, Config::get('admin.menu.rules'), Config::get('admin.menu.message'));
        if ($validator->passes()) {
            $re = (new Menu())->createNew($input);
            if ($re) {
                event(new AdminEvent('create_menu', $re));
                return redirect('admin/menu');
            } else {
                return back()->with('errors', Config::get('admin.errors.createFail'));
            }
        } else {
            return back()->withErrors($validator);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $menu = new Menu();
        $one = $menu->findOneById($id);
        $list = $menu->findAllValid();
        return view('admin.menu_edit', ['menu_info' => $one, 'menu_list' => PhpTreeServiceProvider::makeTreeForHtml($list)]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = Input::except('_token', '_method');

        $validator = $this->formValidator($input, Config::get('admin.menu.rules'), Config::get('admin.menu.message'));
        if ($validator->passes()) {
            $old_menu = (new Menu())->findOneById($id)->toArray();
            $re = (new Menu())->updateOneById($id, $input);
            if ($re) {
                event(new AdminEvent('edit_menu', $input, $old_menu));
                return redirect('admin/menu');
            } else {
                return back()->with('errors', Config::get('admin.errors.editFail'));
            }
        } else {
            return back()->withErrors($validator);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $old_obj = (new Menu())->findOneById($id);
        $rs = (new Menu())->deleteById($id);
        if($rs){
            event(new AdminEvent('remove_menu',$old_obj,''));
            return redirect('admin/menu');
        }
    }


    public function sort(){
        $input = Input::all();
        $rs = true;
        foreach ($input['menu_ids'] as $key=>$item){
            $rs = (new Menu())->updateOneById($item,[
                'sort' => $input['sort_nums'][$key]
            ]);
        }
        if($rs){
            return redirect('admin/menu');
        }
    }
}
